package com.hbwxz.product.pojo;

import com.sun.xml.internal.ws.api.policy.SourceModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.StringBuilders;
import org.apache.lucene.queryparser.xml.builders.BooleanQueryBuilder;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregator;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.util.List;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ProductSearchBody {
    private String key; //skuTitle
    private Long categoryId;
    private List<Long> brandIds;
    private Long[] price;

    private String sort;//升序还是降序 自定义排序price_asc
    private Integer pageNum;

    private List<String> attrs;//9：35，36 9属性的35，36

    public SearchRequest generateSearchRequest() {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if(StringUtils.isNotEmpty(key)) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("skuTitle",key));
        }

        if(categoryId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("categoryId",categoryId));
        }
        if(brandIds != null && brandIds.size() > 0) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("brandId",brandIds));
        }
        if(price != null) {
            RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("price");
            rangeQuery.gte(price[0]).lte(price[1]);
            boolQueryBuilder.filter(rangeQuery);
        }

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        if(attrs != null && attrs.size() > 0) {
            attrs.forEach(attr -> {
                String[] attrSplit = attr.split(":");
//                queryBuilder.must(QueryBuilders.termQuery("attrs.attrId",attrSplit[0]));
                String[] multiAttrValues = attrSplit[1].split(",");
                queryBuilder.must(QueryBuilders.termsQuery("attrs.attrValue",multiAttrValues));
            });
        }
        NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("attrs",queryBuilder, ScoreMode.None);
        boolQueryBuilder.filter(nestedQueryBuilder);
        sourceBuilder.query(boolQueryBuilder);

        if(StringUtils.isNotEmpty(sort)) {
            String[] sortSplits = sort.split("_");
            sourceBuilder.sort(sortSplits[0], sortSplits[1].equalsIgnoreCase("asc")?SortOrder.ASC:SortOrder.DESC);
        }
        sourceBuilder.from((pageNum-1)*50);
        sourceBuilder.size(50);

        TermsAggregationBuilder brandAgg = AggregationBuilders.terms("brandAgg").field("brandId").size(50);
        TermsAggregationBuilder brandNameAgg = AggregationBuilders.terms("brandNameAgg").field("brandName").size(50);
        brandAgg.subAggregation(brandNameAgg);
        sourceBuilder.aggregation(brandAgg);

        NestedAggregationBuilder nestedAggregationBuilder = new NestedAggregationBuilder("attrs","attrs");
        TermsAggregationBuilder attrIdAgg = AggregationBuilders.terms("attrIdAgg").field("attrs.attrId").size(50);
        TermsAggregationBuilder attrNameAgg = AggregationBuilders.terms("attrNameAgg").field("attrs.attrName").size(50);
        TermsAggregationBuilder attrValueAgg = AggregationBuilders.terms("attrValueAgg").field("attrs.attrValue").size(50);
        attrIdAgg.subAggregation(attrNameAgg);
        attrIdAgg.subAggregation(attrValueAgg);
        nestedAggregationBuilder.subAggregation(attrIdAgg);
        sourceBuilder.aggregation(nestedAggregationBuilder);

        return new SearchRequest(new String[]{"commodity"},sourceBuilder);
    }


}
